package com.fsmv.brainfuck2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FileManager extends ListActivity {
  private List<String> directoryEntries = new ArrayList<String>();
  private File currentDirectory = new File("/sdcard/brainfuck/");
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //set main layout
    setContentView(R.layout.filemanager);
    //browse to root directory
    browseTo(new File("/sdcard/brainfuck/"));
  }
   
  //browse to parent directory
  private void upOneLevel(){
    if(this.currentDirectory.getParent() != null) {
      this.browseTo(this.currentDirectory.getParentFile());
    }
  }
  
  public void finishActivity(File file){
	  Intent i = new Intent();
      i.putExtra("selectedFile", file.getName());
      i.putExtra("selectedDir", file.getPath());
      setResult(RESULT_OK, i);
      finish();
  }
  
  //browse to file or directory
  private void browseTo(final File aDirectory){
    //if we want to browse directory
    if (aDirectory.isDirectory()){
      //fill list with files from this directory
      this.currentDirectory = aDirectory;
      fill(aDirectory.listFiles());
      
      //set titleManager text
      TextView titleManager = (TextView) findViewById(R.id.titleManager);
      titleManager.setText(aDirectory.getAbsolutePath());
    } else {
      //if we want to open file, show this dialog:
      //listener when YES button clicked
      OnClickListener okButtonListener = new OnClickListener(){
        public void onClick(DialogInterface arg0, int arg1) {
          //intent to navigate file
          Log.e("Yes Clicked:", "running finish()");
          finishActivity(aDirectory);
        }
      };
      //listener when NO button clicked
      OnClickListener cancelButtonListener = new OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
          //do nothing
          //or add something you want
        }
      };
      
      //create dialog
      new AlertDialog.Builder(this)
        .setTitle("Confirmation") //title
        .setMessage("Open "+ aDirectory.getName() + "?") //message
        .setPositiveButton("Yes", okButtonListener) //positive button
        .setNegativeButton("No", cancelButtonListener) //negative button
        .show(); //show dialog
    }
  }
  //fill list
  private void fill(File[] files) {
    //clear list	
    this.directoryEntries.clear();  
       
    if (this.currentDirectory.getParent() != null)
      this.directoryEntries.add("..");
       
    //add every file into list
    for (File file : files) {
      this.directoryEntries.add(file.getAbsolutePath());
    }
    
    //create array adapter to show everything
    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.row, this.directoryEntries);
    this.setListAdapter(directoryList);
  }
  //when you clicked onto item
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
	super.onListItemClick(l, v, position, id);
    //get selected file name
    int selectionRowID = position;
    String selectedFileString = this.directoryEntries.get(selectionRowID);
    
    //if we select ".." then go upper
    if(selectedFileString.equals("..")){
      this.upOneLevel();
    } else {
      //browse to clicked file or directory using browseTo()
      File clickedFile = null;
      clickedFile = new File(selectedFileString);
      if (clickedFile != null)
        this.browseTo(clickedFile);
    }
  }
}